package com.yupi.springbootinit.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Configuration
public class ThreadPoolExcuterConfig {


    @Bean
    public ThreadPoolExecutor threadPoolExecutor() {

        //创建线程工厂
        ThreadFactory threadFactory = new ThreadFactory() {
            private int count = 1;

            @Override
            public Thread newThread(Runnable r) {
            Thread thread = new Thread(r);

            thread.setName("线程" + count++);
            return thread;
            }
        };
        ThreadPoolExecutor executor = new ThreadPoolExecutor( 2, 4, 100, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(4),threadFactory);


        return executor;
    }

}
